import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {
    public static int[] generateAnswer(){
        Random random = new Random();
        int[] arr = new int[4];
        arr[0] = random.nextInt(10);
        for (int i = 1 ; i < arr.length ; i ++){
            arr[i] = random.nextInt(10);
            for (int j = 0 ; j <i; j++){
                if (arr[i] ==arr[j]){
                    arr[i] = random.nextInt(10);
                    j=-1;
                }
            }
        }
        for (int i : arr){
            System.out.print(i);
        }
        return arr;
    }
    public static int[] getPlayerInput(int[] b){
        return b;
    }

    public static String compareGuessAnswer(int[] b,int[] c){
        int[] date = getPlayerInput(b);
        int count =0;
        String total="";
        int A = 0;int B = 0;
        for (int i = 0 ; i < date.length ; i++){
                if (date[i] == c[i]){
                    A++;
                }else {
                    B++;
                }

        }
        if(A==4){
            total = "猜对了";
        }
            total = total+A+"A"+B+"B";
        System.out.println(total);
        return total;
    };
    public static boolean isWin(String date){
        return date.contains("4A");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] b = new int[4];

            int count = 0;
        while (true){
            System.out.println("只有七次机会哦");
            while (true){
                int[] all = generateAnswer();
                System.out.println("A表示存在且位置正确的数字个数，B表示存在但位置不正确的位置个数");
                System.out.println("如：1A2B 代表猜测的三个数字是对的有一个位置对，有两个位置不对");
                System.out.println("请输入四个数：");
                while (true){
                    for (int i = 0; i <b.length; i ++){
                        System.out.println("请输入第"+(i+1)+"个数：");
                        b[i] = scanner.nextInt();
                    }
                    if (b[0]!=b[1]&&b[1]!=b[2]&&b[2]!=b[3]){
                        break;
                    }else {
                        System.out.println("输入数字不能重复！请重新输入！！");
                    }
                }

                if (isWin(compareGuessAnswer(all,b))) {
                    break;
                }


        }
            System.out.println("重玩请输入零，输入其他游戏结束");
            int start = scanner.nextInt();
            if (start!=0){
                break;
            }
    }

    }
}

